define(
    ["PDUtilDir/dataService", "PDUtilDir/grid", "AppPathDir/order/orderForVisitorProductInfos/addVisitor", "PDUtilDir/util", "PDUtilDir/tool", "PDUtilDir/slidebar", "PDUtilDir/inputSelect", "Date", "DateCN", "css!DateCss"],
    function (DataService, Grid, AddVisitorSupport, Util, Tool, Slidebar, inputSelect) {
        function agencySupportInit($scope, $compile) {
            return new agencySupport($scope, $compile);
        };

        function agencySupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;
        };
        agencySupport.fn = agencySupport.prototype;

        agencySupport.fn.init = function (id) {
            var _this = this;

            _this.entity = {
                id: id,
                visitors: new Array()
            };

            _this.cache = {
                certificates: new Array(),
                certificatesObject: new Map()
            };

            $.ajax({
                url : getServer() + "/flea/hclv/getCertificates.htmls",
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.cache.certificates = data.data;
                    $.each(data.data, function(index, row) {
                        _this.cache.certificatesObject.set(row.dictCode, row.dictName);
                    });
                }
            });
        };

        agencySupport.fn.digest = function () {
            this.$scope.$digest();
        };

        agencySupport.fn.run = function (id) {
            var _this = this;
            _this.init(id);

            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/order/orderForVisitorProductInfos/views/add.html",
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        agencySupport.fn.initialization = function () {
            this.baseInit();
            this.visitorInfoInit();
        };
        agencySupport.fn.baseInit = function () {
            var _this = this;
        };

        /* 添加车辆 */
        agencySupport.fn.addVisitorSupportFun = function (){
            if(this.addVisitorSupport)
                return this.addVisitorSupport;
            return this.addVisitorSupport = AddVisitorSupport(this.$scope, this.$compile)
        };

        // 刷新车辆列表
        agencySupport.fn.reloadVisitorsSupport = function(){
            var _this = this;
            return {
                run : function() {
                    var selecteds = _this.entity.visitors,
                        datas = DataService.getObject('product.agency.search.visitors');

                    $.each(datas, function(index, row) {
                        selecteds.push($.extend(row, {
                            uuid: new Date().getTime(),
                            visitorId: row.id
                        }));
                    });
                    _this.gird.reload();
                }
            }
        };
        // 删除车辆
        agencySupport.fn.removeVisitorSupport = function(){
            var _this = this;
            return {
                run : function(uuid) {
                    var selecteds = _this.entity.visitors;

                    $.each(selecteds, function(index, row) {
                        if (row.uuid == uuid){
                            selecteds.splice(index, 1);
                            return;
                        }
                    });
                    _this.gird.reload();
                }
            };
        };
        agencySupport.fn.save = function() {
            var _this = this, visitorIds = new Array();
            $.each(_this.entity.visitors, function(index, row) {
                visitorIds.push(row.id);
            });
            $.ajax({
                url : getServer() + "/flea/hclv/productAgency/addAgency.htmls",
                data : {
                    'model' : JSON.stringify({
                        visitorIds: visitorIds,
                        timeTableId: _this.entity.id,
                        prouductId: _this.$scope.entity.product.id,
                        agencyedDept: _this.entity.agencyedDept,
                        totalSale: _this.entity.totalSale,
                        totalCost: _this.entity.totalCost
                    })
                },
                success : function(data) {
                    if (data.status != 200) {
                        return;
                    }
                    _this.$scope.grid.refresh();
                    Util.alert(data.message);
                    _this.slidebars.close();
                }
            });
        };
        agencySupport.fn.visitorInfoInit = function () {
            var _this = this;

            _this.gird = Grid({
                id: "agencyVisitorGirdDivId",
                pageSize: 10,
                index: true,
                multi: false,
                pagination: true,
                cache: false,
                layout : [ {
                    name : "中文名",
                    field : "zhName",
                    width: "60px",
                    sort : true
                }, {
                    name : "英文名",
                    field : "enName",
                    width: "60px",
                    sort : true
                }, {
                    name : "性别",
                    field : "sex",
                    width: "40px",
                    format : function(e) {
                        if (e.row.sex == "1" || e.row.sex == 1)
                            return '男';
                        else
                            return '女';
                    }
                }, {
                    name : "年龄",
                    width: "40px",
                    field : "age"
                }, {
                    name : "联系电话",
                    width: "80px",
                    field : "phone"
                }, {
                    name : "证件类型",
                    field : "certificates",
                    width: "60px",
                    format:function(e){
                        return _this.cache.certificatesObject.get(e.row.certificates);
                    }
                }, {
                    name : "证件号码",
                    field : "certificatesNumber"
                } ],
                toolbar: [
                    {
                        name: "添加",
                        icon: "fa fa-search",
                        callback: function (event) {
                            _this.addVisitorSupportFun().run();
                        }
                    }
                ],
                data: this.entity.visitors
            });
        };
        return agencySupportInit;
    });